package org.black.phoneUtility.app.activity;

import org.black.phoneUtility.PhoneUtilityConstant;
import org.black.phoneUtility.R;
import org.black.phoneUtility.widget.ContactGroupCursorAdapter;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class ContactGroupActivity extends Activity implements
		AdapterView.OnItemClickListener, LoaderCallbacks<Cursor> {

	public static final String TAG = ContactGroupActivity.class.getName();
	private static final String LOG_TAG = ContactGroupActivity.class.getName();

	public static final String RESULT_GROUP_ID = "result_group_id";

	private ListView contactGroupListView;
	private ContactGroupCursorAdapter contactGroupCursorAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.contact_group_list);

		this.contactGroupCursorAdapter = new ContactGroupCursorAdapter(this,
				null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		this.contactGroupListView = (ListView) findViewById(R.id.contactGroupListView);
		View emptyView = findViewById(R.id.contactGroupListViewNoDataTextView);
		this.contactGroupListView.setEmptyView(emptyView);
		this.contactGroupListView.setAdapter(contactGroupCursorAdapter);
		this.contactGroupListView.setOnItemClickListener(this);
		this.contactGroupCursorAdapter.notifyDataSetChanged();

		getLoaderManager().initLoader(
				PhoneUtilityConstant.INIT_LOADER_CONTACT_GROUP_ID, null, this);
	}

	@Override
	protected void onDestroy() {
		this.contactGroupCursorAdapter = null;
		this.contactGroupListView = null;
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = getIntent();
		String action = intent.getAction();
		if (Intent.ACTION_PICK.equals(action)
				|| Intent.ACTION_GET_CONTENT.equals(action)) {
			Uri uri = ContentUris.withAppendedId(
					ContactsContract.Groups.CONTENT_URI, id);
			Intent resultIntent = new Intent();
			resultIntent.setData(uri);
			resultIntent.putExtra(RESULT_GROUP_ID, id);
			setResult(RESULT_OK, resultIntent);
			onBackPressed();
		}
		Log.i(LOG_TAG, "action: " + action);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new CursorLoader(this,
				ContactsContract.Groups.CONTENT_SUMMARY_URI, null,
				ContactsContract.Groups.DELETED + " = ? ",
				new String[] { String.valueOf(0) }, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		if (data != null && data.moveToFirst()) {
			if (contactGroupCursorAdapter != null) {
				this.contactGroupCursorAdapter.swapCursor(data);
				this.contactGroupCursorAdapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		if (contactGroupCursorAdapter != null) {
			contactGroupCursorAdapter.swapCursor(null);
		}
	}

}
